//
//  WXBase64Module.swift
//  mlz
//
//  Created by 吕凌 on 2018/4/14.
//  Copyright © 2018年 吕凌. All rights reserved.
//

import Foundation

public extension WXBase64Module {
    
    @objc public func encodeUrlData(_ urlstr: String, callback: @escaping WXModuleKeepAliveCallback) {

        let url = URL(string:urlstr)
        //创建请求对象
        let request = URLRequest(url: url!)
        
        let session = URLSession.shared
        let dataTask = session.dataTask(with: request,
                                        completionHandler: {(data, response, error) -> Void in
                                            let base64_str = data?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
                                            callback(base64_str!, false)
        }) as URLSessionTask
        
        //使用resume方法启动任务
        dataTask.resume()

    }

    @objc public func encodeData(_ str: String, callback: @escaping WXModuleKeepAliveCallback) {

        let base64_str = str.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
        callback(base64_str, false)

    }
}
